﻿using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp;

namespace Acme.BookStore2.Controllers
{
    [RemoteService(Name = "Default")]
    [Area("bookstore2")]
    [ControllerName("Blob")]
    [Route("api/bookstore2/blob")]
    public class BlobController : BookStore2Controller
    {
        private readonly IBlobAppService _blobAppService;

        public BlobController(IBlobAppService blobAppService)
        {
            _blobAppService = blobAppService;
        }

        [HttpPost]
        [Route("{blobContainerName}/upload")]
        public async Task<BlobDto> CreateAsync(string blobContainerName, IFormFile file)
        {
            return await _blobAppService.CreateAsync(new CreateBlobInput
            {
                FileName = file.FileName,
                BlobContainerName = blobContainerName,
                Bytes = await file.GetAllBytesAsync()
            });
        }

        [HttpGet]
        [Route("{blobContainerName}/{blobName}")]
        public async Task<byte[]> GetAsync(string blobContainerName, string blobName)
        {
            return await _blobAppService.GetAllBytesOrNullAsync(blobContainerName, blobName);
        }
    }
}